<?php

require 'facebook.php';
require 'config.inc.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $fbconfig['app_id'],
  'secret' => $fbconfig['secret_key'],
  'cookie' => true,
));

$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
  } catch (FacebookApiException $e) {
    echo "Somethings wrong";
  }
}

$id = $_POST["uid"];
$access_token = $_POST["access_token"];
$page=$_POST["page"];
if($page==NULL || $page=="")
	$page="recent";
	
try{
	$fql    =   'SELECT uid FROM user WHERE is_app_user="true" and uid in (select uid2 from friend where uid1='.$uid.')';
	$param  =   array(
		'method'    => 'fql.query',
		'query'     => $fql,
		'callback'  => ''
	);
	$result   =   $facebook->api($param);
}catch(Exception $o){
	echo($o);
}

for($i=0,$txt="";$i<sizeof($result);$i++)
{
	$txt= $txt.$result[$i]["uid"].',';
}

$txt.=$uid;

//$txt.=$id;
//echo $uid;
//echo $groupid;
$content =
	"<fb:name uid='".$uid."' firstnameonly='true' shownetwork='false'/> has started using <a href='http://apps.facebook.com/linkstar/index.php'>Linkstar</a> application and thought it's so cool even you should try it out!\n".
	"<fb:req-choice url='http://apps.facebook.com/linkstar/index.php' label='Join Linkstar'/>";
		


?>

<fb:serverfbml id="inv" width="610">
	<script type="text/fbml">
		<fb:request-form action="http://studentweb.comminfo.rutgers.edu/2010-9-673-01/vatsav/index.php?p=<?php echo $page;?>" 
			method="POST" 
			invite="true" 
			type="Linkstar" 
			content="<?php echo htmlentities($content,ENT_QUOTES,'UTF-8'); ?> ">
			
			<fb:multi-friend-selector 
				email_invite="false" 
				rows="5" cols="4" 
				showborder="false" 
				bypass="cancel" 
				cols=4 
				actiontext="Invite Friends to Linkstar" 
				exclude_ids="<?php echo $txt; ?>"/> 
		</fb:request-form>
	</script>
</fb:serverfbml>
<?php

mysql_close($con);

?>